<?php 
class ManageBookingVip extends Module{
	function ManageBookingVip($row){
		Module::Module($row);
		
		if(User::have_permit(ADMIN_VIP_ITEM) || User::have_permit(ADMICRO_BOOKING_VIP)){
			$cmd=Url::get('cmd');
			switch($cmd){				
				case '':
					require_once 'forms/ManageBookingVip.php';
					$this->add_form(new ManageBookingVipForm());
					break;
					
				case 'add':
					require_once 'forms/AddBookingVip.php';
					$this->add_form(new AddBookingVipForm());
					break;
				case 'active':
                    if(User::have_permit(ADMIN_VIP_ITEM)){
                        $id = (int)Url::get("id",0);
						if($id){
							$booking_vip = DB::select("bookings_vip","id=$id");
						}
                        ($booking_vip['top_cat'] != 0) ? $cat_id=$booking_vip['top_cat'] : $cat_id = $booking_vip['cat_id'];
                        
                        $item_vip = DB::select_all('item_vip', "(catid = $cat_id OR level_1_catid = $cat_id) AND status = 1");

                        $i = 0;
                        $sa = $booking_vip['time_start'];
                        $ea = $booking_vip['time_end'];

                        foreach($item_vip as $itemv){
                            if($sa > $itemv['time_start'] && $sa < $itemv['time_end']){
                                $i++;
                            }elseif($ea > $itemv['time_start'] && $ea < $itemv['time_end']){
                                $i++;
                            }elseif($sa < $itemv['time_start'] && $ea > $itemv['time_end']){
                                $i++;
                            }
                        }

                        $priority = 1;
                        $arrItemVip = array(
										'item_id'  		=> $booking_vip['item_id'],
										'note'			=> "Add từ bảng booking vip cho [{$booking_vip['user_add']}]",
										'time_start'	=> $booking_vip['time_start'],
										'time_end' 		=> $booking_vip['time_end'],
										'time_add' 		=> TIME_NOW,
										'user_add' 		=> User::user_name(),
										'time_modify' 	=> TIME_NOW,
										'user_modify' 	=> User::user_name(),
										'catid'			=> $booking_vip['cat_id'],
										'level_1_catid'	=> $booking_vip['top_cat'],
                                        'num_contract'  => $booking_vip['num_contract'],
                                        'priority' 		=> $priority,
									);
                            
                            //insert
                            $idVip = DB::insert("item_vip",$arrItemVip);

                            if($idVip){
                                $item_id = $booking_vip['item_id'];
                                $item = Item::get_item($item_id);
                                
                                if($booking_vip['time_start'] >= TIME_NOW || $booking_vip['time_end'] <= TIME_NOW)
                                {
                                    $priority  = 0;
                                }

                                DB::update("item",array("sticky"=>$priority),"id=$item_id");

                                if(MEMCACHE_ON){
                                    $item['sticky']  = $priority;
                                    AZMemcache::do_put("item:$item_id",$item);

                                    AZMemcache::do_remove("id_vip:{$item['category_id']}");
                                    AZMemcache::do_remove("id_vip:{$item['level_1_catid']}");
                                }
                            }

                            //update status
                            DB::update('bookings_vip', array("status" => 1,"time_update_status" => TIME_NOW, "user_update_status" => User::user_name()), "id=$id");
                    }
                    Url::redirect_current();
					break;
				
				case 'update_status':
                    if(User::have_permit(ADMIN_VIP_ITEM)){
                        $id = (int)Url::get("id",0);
                        DB::update('bookings_vip', array("status" => 1,"time_update_status" => TIME_NOW, "user_update_status" => User::user_name()), "id=$id");
                    }
                    Url::redirect_current();
					break;	
					
				case 'edit':
					require_once 'forms/EditBookingVip.php';
					$this->add_form(new EditBookingVipForm());
					break;
					
				case 'delete':
					$id = (int)Url::get("id",0);

					$booking_vip = array();

					if($id){
						$booking_vip = DB::select("bookings_vip","id=$id");
					}
                    if($booking_vip && $booking_vip['status'] != 1){
                        if(User::user_name() == $booking_vip['user_add'] || User::have_permit(ADMIN_VIP_ITEM)){
                            DB::update('bookings_vip', array("status" => -1,"time_del" => TIME_NOW, "user_del" => User::user_name()), "id=$id");
                        }
                    }
                    
					Url::redirect_current();
					break;
				default:
					Url::redirect_current();
					break;
			}
		}
		else{
			Url::access_denied();
		}
	}
}
?>